#include<iostream>
using namespace std;
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct ListNode
{
	int val;
	struct ListNode* next;
};
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution
{
public:
    int getDecimalValue(ListNode* head)
    {
		ListNode* prev = nullptr;
		ListNode* cur = nullptr;
		ListNode* tmp = head;
        while (tmp != nullptr)
        {
            prev = cur;
            cur = tmp;
			tmp = tmp->next;
            cur->next = prev;    
        }
        ListNode* p = cur;
        int i = 0;
		int sum = 0;
        while (p)
        {
            sum += (p->val << i);
            i++;
            p = p->next;
        }
        return sum;
    }
};


int main()
{
	Solution s;
	struct ListNode* n1 = (struct ListNode*)malloc(sizeof(struct ListNode));
	struct ListNode* n2 = (struct ListNode*)malloc(sizeof(struct ListNode));
	struct ListNode* n3 = (struct ListNode*)malloc(sizeof(struct ListNode));
	struct ListNode* n4 = (struct ListNode*)malloc(sizeof(struct ListNode));
	n1->val = 1;
	n2->val = 0;
	n3->val = 1;
	n4->val = 1;
	n1->next = n2;
	n2->next = n3;
	n3->next = n4;
	n4->next = NULL;
	printf("%d -> ", n1->val);
	printf("%d -> ", n1->next->val);
	printf("%d -> ", n1->next->next->val);
	printf("%d -> \n", n1->next->next->next->val);
	int ret = s.getDecimalValue(n1);
	cout << ret << endl;
	free(n1);
	n1 = NULL;
	free(n2);
	n2 = NULL;
	free(n3);
	n3 = NULL;
	free(n4);
	n4 = NULL;

}